package common

import (
	"fmt"
	"strings"
	"time"

	"github.com/fatih/color"
)

// PrintStatistics 美化输出任务统计信息
func PrintStatistics(stats *TaskStattistics) {
	// 创建不同颜色的打印函数
	titleColor := color.New(color.FgBlue, color.Bold)
	headerColor := color.New(color.FgGreen, color.Bold)
	tableHeaderColor := color.New(color.FgCyan, color.Bold)
	tableNormalColor := color.New(color.FgWhite, color.Bold)

	titleColor.Println("======================================================================================================")

	// 如果有表统计信息，则以表格形式输出
	if len(stats.TableStats) > 0 {
		// 表头
		//verify
		if strings.Contains(stats.TaskType, "ver") {
			tableHeaderColor.Printf("%-25s %-20s %-20s %-8s %-8s %-8s %-8s\n",
				"Table Name", "Start Time", "Duration", "Count", "Added", "Updated", "Deleted")
		} else {
			tableHeaderColor.Printf("%-25s %-20s %-20s %-10s\n",
				"Table Name", "Start Time", "Duration", "Count")
		}
		titleColor.Println("------------------------------------------------------------------------------------------------------")

		// 表数据
		for _, tableStat := range stats.TableStats {
			durationStr := truncateString(tableStat.EndTime.Sub(tableStat.StartTime).Truncate(time.Second).String(), 10)
			if tableStat.Error != "" {
				durationStr = "FAILED!"
			}
			if strings.Contains(stats.TaskType, "ver") {
				tableNormalColor.Printf("%-25s %-20s %-20s %-8d %-8d %-8d %-8d",
					truncateString(GetFullTableName(tableStat.DBName, tableStat.SchemaName, tableStat.TableName), 25),
					tableStat.StartTime.Format("2006-01-02 15:04:05"),
					durationStr,
					tableStat.Count,
					tableStat.AddedRows,
					tableStat.UpdatedRows,
					tableStat.DeletedRows)
			} else {

				tableNormalColor.Printf("%-25s %-20s %-20s %-12d",
					truncateString(GetFullTableName(tableStat.DBName, tableStat.SchemaName, tableStat.TableName), 25),
					tableStat.StartTime.Format("2006-01-02 15:04:05"),
					durationStr,
					tableStat.Count)
			}

			fmt.Println() // 换行

		}
		titleColor.Println("======================================================================================================")
	}
	// 输出任务基本信息
	titleColor.Println("========================================== TASK STATISTICS ===========================================")
	headerColor.Printf("任务名(Task Name):        ")
	fmt.Printf("%-30s\n", stats.TaskName)
	headerColor.Printf("任务类型(Task Type):      ")
	fmt.Printf("%-30s\n", stats.TaskType)
	headerColor.Printf("开始时间(Start Time):     ")
	fmt.Printf("%-30s\n", stats.StartTime.Format("2006-01-02 15:04:05"))
	headerColor.Printf("结束时间(End Time):       ")
	fmt.Printf("%-30s\n", stats.EndTime.Format("2006-01-02 15:04:05"))
	headerColor.Printf("总表数(Table Count):      ")
	fmt.Printf("%-30d\n", len(stats.TableStats))
	headerColor.Printf("耗时(Duration):           ")
	fmt.Printf("%-30s\n", stats.EndTime.Sub(stats.StartTime).Truncate(time.Second).String())
	titleColor.Println("======================================================================================================")
}

// truncateString 截断字符串到指定长度
func truncateString(s string, maxLen int) string {
	if len(s) <= maxLen {
		return s
	}
	if maxLen > 3 {
		return s[:maxLen-3] + "..."
	}
	return s[:maxLen]
}
